function validateFormBeforeSubmitForAddEmployee(theForm) {
	var reason = "";
	var employeeCode = validateMe(document.getElementById('employeeCode'), 'Employee Code');
	reason += employeeCode;
	var employeeName = validateMe(document.getElementById('employeeName'), 'Employee Name');
	reason += employeeName;
	var emailId = validateMe(document.getElementById('emailId'), 'Email Id');
	reason += emailId;
	var password = validateMe(document.getElementById('password'), 'Password');
	reason += password;
	var roleId = validateMe(document.getElementById('roleId'), 'Role Id');
	reason += roleId;

	if (reason != "") {
		alert("Errors:\n" + reason);
		return false;
	}
	document.forms["addEmployee"].action = "employeeMaintenance.html?method=saveEmployee";
	document.forms.addEmployee.submit();
}

function validateMe(fld, name) {
	var error = "";

	if (fld.value == null || (fld.value != null && (fld.value.length == 0 || fld.value == "Select"))) {
		fld.style.background = 'Yellow';
		error = name + " field has not been filled in.\n";
	} else {
		var numaric = fld.value;
		var alphaa = numaric.charAt(0);
		var chCode = alphaa.charCodeAt(0);
		if (chCode == "32") {
			fld.style.background = 'Yellow';
			error = name + " should not start with space.\n";
		}
		if (name == "Password") {
			if (fld.value.length > 20) {
				error = name + " should not be more than 20 characters.\n";
			} else if (fld.value.length < 6 && fld.value.length > 0) {
				error = name + " should not be less than 6 characters.\n";
			}
		}
		if (name == "Email Id") {
			var at = "@";
			var dot = ".";
			var lat = fld.value.indexOf(at);
			var lstr = fld.value.length;
			var ldot = fld.value.indexOf(dot);
			var invalidChar = "!#$%^&*()}{][|\+=`~;:><?/, ";
			var icl = invalidChar.length;

			for ( var i = 0; i < lstr - 1; i++) {
				for ( var j = 0; j < icl - 1; j++) {
					if (fld.value.substring(i, i + 1) == invalidChar.substring(j, j + 1)) {
						fld.style.background = 'Yellow';
						error = name + " is not valid.\n";
					}
				}
			}

			for ( var i = 0; i < lstr - 2; i++) {
				if (fld.value.substring(i, i + 1) == dot && fld.value.substring(i + 1, i + 2) == dot) {
					fld.style.background = 'Yellow';
					error = name + " is not valid.\n";
				}
			}

			if (fld.value.indexOf(at) == -1) {
				fld.style.background = 'Yellow';
				error = name + " is not valid.\n";

			}

			if (fld.value.indexOf(at) == -1 || fld.value.indexOf(at) == 0 || fld.value.indexOf(at) == lstr) {
				fld.style.background = 'Yellow';
				error = name + " is not valid.\n";

			}

			if (fld.value.indexOf(dot) == -1 || fld.value.indexOf(dot) == 0 || fld.value.indexOf(dot) == lstr) {
				fld.style.background = 'Yellow';
				error = name + " is not valid.\n";
			}

			if (fld.value.indexOf(at, (lat + 1)) != -1) {
				fld.style.background = 'Yellow';
				error = name + " is not valid.\n";
			}

			if (fld.value.substring(lat - 1, lat) == dot || fld.value.substring(lat + 1, lat + 2) == dot) {
				fld.style.background = 'Yellow';
				error = name + " is not valid.\n";
			}

			if (fld.value.indexOf(dot, (lat + 2)) == -1) {
				fld.style.background = 'Yellow';
				error = name + " is not valid.\n";
			}

			if (fld.value.indexOf(" ") != -1) {
				fld.style.background = 'Yellow';
				error = name + " is not valid.\n";
			}

		}
	}
	return error;
}

function cursorPosition(jspDirection) {
	if (jspDirection == "MaintenanceMenu") {
		document.getElementById('employeeCode').value = "";
		document.getElementById('employeeCode').focus();
	} else {
		if (document.getElementById('maintable').style.visibility == 'visible') {
			var fld = document.getElementById('employeeCode');
			if (fld.value.length == 0) {
				document.getElementById('employeeCode').focus();
			} else {
				document.getElementById('employeeName').focus();
			}
		}
	}
}

function checkValidity() {
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var name = document.getElementById('employeeCode').value;
	var error = 0;

	var IsValidChar = true;
	var Char;
	for (i = 0; i < name.length && IsValidChar == true; i++) {
		Char = name.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			error = 1;
		}
	}

	if (error == 1) {
		alert("Employee Code is not valid");
		document.getElementById('employeeCode').value = "";
		document.getElementById('employeeCode').focus();
	} else {
		document.forms["addEmployee"].action = "employeeMaintenance.html?method=checkForEmployeeAvailibility";
		document.forms.addEmployee.submit();
	}
}

function changeShadeForEC() {
	document.getElementById('employeeCode').style.background = 'White';
	document.getElementById('employeeCode').focus();
}

function changeShadeForEN() {
	document.getElementById('employeeName').style.background = 'White';
	document.getElementById('employeeName').focus();
}

function changeShadeForEmail() {
	document.getElementById('emailId').style.background = 'White';
	document.getElementById('emailId').focus();
}

function changeShadeForPass() {
	document.getElementById('password').style.background = 'White';
	document.getElementById('password').focus();
}

function changeShadeForRole() {
	document.getElementById('roleId').style.background = 'White';
	document.getElementById('roleId').focus();
}

function changeShadeForPass() {
	document.getElementById('password').style.background = 'White';
	document.getElementById('password').focus();
}

function validateEmailForUser() {
	var error = 0;
	if (document.getElementById('emailId').value != null && document.getElementById('emailId').value.length != 0) {
		var remValue = document.getElementById('emailId').value;
		error = validateEmail(remValue);
		if (error == 1) {
			alert("Email id is not valid.");
			document.getElementById('emailId').focus();
		}

	}
}

function validateReminderEmailCc() {

	if (document.getElementById('reminderEmailCc').value != null
			&& document.getElementById('reminderEmailCc').value.length != 0) {
		var remValue = document.getElementById('reminderEmailCc').value;
		var arr = remValue.split(",");
		var len = arr.length;
		var breakLoop = 0;
		for ( var i = 0; i < arr.length; i++) {
			breakLoop = validateEmail(arr[i]);
			if (breakLoop == 1) {
				alert("One of the reminder email ids is not valid.");
				document.getElementById('reminderEmailCc').focus();
				break;
			}
		}

	}

}
function validatePassportNo() {
	var passNo = document.getElementById('passportNo').value;
	var invalidChar = ".!#$%^&*(){}][|\+=`~;:><?/, ";
	var icl = invalidChar.length;
	var error = 0;
	var lstr = passNo.length;
	for ( var i = 0; i < lstr; i++) {
		for ( var j = 0; j < icl - 1; j++) {
			if (passNo.substring(i, i + 1) == invalidChar.substring(j, j + 1)) {
				error = 1;

			}
		}
	}
	if (error == 1) {
		alert("Passport no. is not valid");
		document.getElementById('passportNo').focus();
	}
}
function validateEmail(str) {
	var error = 0;
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	var invalidChar = "!#$%^&*()}{][|\+=`~;:><?/, ";
	var icl = invalidChar.length;

	for ( var i = 0; i < lstr - 1; i++) {
		for ( var j = 0; j < icl - 1; j++) {
			if (str.substring(i, i + 1) == invalidChar.substring(j, j + 1)) {
				error = 1;
			}
		}
	}

	for ( var i = 0; i < lstr - 2; i++) {
		if (str.substring(i, i + 1) == dot && str.substring(i + 1, i + 2) == dot) {
			error = 1;
		}
	}

	if (str.indexOf(at) == -1) {
		error = 1;

	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
		error = 1;

	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
		error = 1;
	}

	if (str.indexOf(at, (lat + 1)) != -1) {
		error = 1;
	}

	if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
		error = 1;
	}

	if (str.indexOf(dot, (lat + 2)) == -1) {
		error = 1;
	}

	if (str.indexOf(" ") != -1) {
		error = 1;
	}

	return error;
}
